package com.pkk.penguin.frame.know.boot.config.handle.zhiyi.db.support.stragety;

import com.alibaba.fastjson.JSONObject;
import com.pkk.penguin.frame.know.boot.config.handle.zhiyi.db.support.util.ExecUtil;
import com.pkk.penguin.support.ssh.properties.SSHConnectionProperties;
import com.pkk.penguin.support.ssh.strategy.SSHConnectionGetStrategy;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * @description: ssh链接数据库的简单实现
 * @author: peikunkun
 * @create: 2020-02-27 16:43
 **/
@Slf4j
@Component
public class SSHConectionMysqlStragety extends SSHConnectionGetStrategy {


  /**
   * @Description: 加载ssh配置文件
   * @Param: sshName
   * @return: com.pkk.penguin.support.ssh.properties.SSHConnectionProperties
   * @Author: peikunkun
   * @Date: 2020/6/1 0001 下午 3:48
   */
  @Override
  public SSHConnectionProperties loadSSHConnectionProperties(String sshName) {
    final JSONObject jsonObject = ExecUtil.PINGTAI_KEY_INFO;
    final Set<String> sshNameSet = jsonObject.keySet();
    if (sshNameSet.contains(sshName)) {
      JSONObject pingtaiJson = jsonObject.getJSONObject(sshName);
      SSHConnectionProperties sshConnectionProperties = new SSHConnectionProperties();
      sshConnectionProperties.setSshHost(pingtaiJson.getString("sshHost"));
      final String sshPort = pingtaiJson.getString("sshPort");
      if (StringUtils.isNotBlank(sshPort)) {
        sshConnectionProperties.setSshPort(Integer.valueOf(sshPort));
      }
      sshConnectionProperties.setSshPwd(pingtaiJson.getString("sshPwd"));
      sshConnectionProperties.setDbHost(pingtaiJson.getString("dbHost"));
      sshConnectionProperties.setDbName(pingtaiJson.getString("dbName"));
      sshConnectionProperties.setDbUserName(pingtaiJson.getString("dbUserName"));
      sshConnectionProperties.setDbUserPwd(pingtaiJson.getString("dbUserPwd"));
      return sshConnectionProperties;
    } else {
      return null;
    }
  }
}
